package com.ciaojian.client.controller;

import com.ciaojian.client.service.MemberServiceImpl;
import com.ciaojian.core.model.Order;
import com.ciaojian.core.util.LogUtil;
import com.ciaojian.core.util.Result;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.*;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author Atlakyn
 */
@Api(tags = "会员")
@RestController
@RequestMapping("/members")
public class MemberController {
    @Resource
    private MemberServiceImpl memberService;

    @ApiOperation("获取折扣")
    @GetMapping("/discount/{orderId}")
    public Result getDiscount(@PathVariable @ApiParam("订单Id") String orderId) {
        Order order = new Order().selectById(orderId);
        Assert.notNull(order, "订单不存在");
        Map<String, String> memberDiscount = memberService.getMemberDiscount(order.getType());
        return Result.ok(memberDiscount);
    }

    @ApiOperation("查找用户会员信息")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "userId", value = "用户id", paramType = "form", dataType = "int"),
            @ApiImplicitParam(name = "title", value = "服务标题", paramType = "form")})
    @PostMapping("/findUserMemberCard")
    public Result findUserMemberCard(Integer userId, String title) {
        Assert.notNull(userId, "用户ID不能为空");
        try {
            return Result.ok(memberService.getMemberByUser(userId, title));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            LogUtil.writeErrorLog(e.getMessage());
            return Result.error("系统错误");
        }
    }
}

